<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\admin\model\config\Agreement;
use app\admin\model\config\Basics;
use app\admin\model\config\Subtitle;
use app\admin\model\goods\Goods;
use app\admin\model\goods\ShopGoodsSpecs;
use app\admin\model\order\Userorder;
use app\admin\model\shop\Shop;
use app\admin\model\user\Address;
use app\common\controller\Api;
use app\common\helpers\ImageUtil;
use app\common\helpers\MapUtil;
use think\Db;
use function fast\e;

/**
 * 定时任务
 */
class Task extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function orderCancel()
    {
        self::unpaidOrder();
        echo '定时取消未支付订单处理完成';exit;

    }
    public static function unpaidOrder(){
        $Order = new \app\admin\model\shop\Order();
        $list = $Order->where('status',0)->select();
        foreach ($list as $key => $val) {
            $endTime=date('Y-m-d H:i:s', strtotime($val->order_time) + 30*60);
            if ($endTime < date("Y-m-d H:i:s")) {
                $Order->where('id', $val['id'])->update(['status' => 11,'end_time'=>$endTime]);//修改订单状态为已取消
            }
        }
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 过期会员处理
     */
    public function memberOverdue(){
        $model = new \app\admin\model\User();
        $list = $model->where("member_expire_time","<",date('Y-m-d H:i:s'))->select();
        if($list){
            foreach ($list as $key => $val) {
                $model->where('id', $val['id'])->update(['member_expire_time'=>null]);
            }
            echo '处理完成';exit();
        }else{
            echo '未更新数据';exit();
        }
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 对已送达的订单 30分钟后自动已完成
     */
    public function orderAutoComplete(){
        $model = new \app\admin\model\shop\Order();
        $list = $model
            ->where('status','6')
            ->where("service_time",">",date('Y-m-d H:i:s',time() - 1800))
            ->select();
        if($list){
            Db::startTrans();
            $Basics = Basics::get(1);
            foreach ($list as $key => $val) {
                $model->where('id', $val['id'])->update(['harvest_time'=>$val['service_time'],'end_time'=>$val['service_time'],'status'=>'7']);
                if($val['is_hy_order'] == 1 && $Basics['distribution_lvl'] != '1'){
                   $rest =  Common::distribution($val); //分销
                    if(!$rest){
                        Db::rollback();
                    }
                }
            }
            Db::commit();
            echo '处理完成';exit();
        }else{
            echo '未更新数据';exit();
        }
    }



}
